<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告标题" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入广告标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="广告位置" prop="subType">
        <el-select v-model="queryParams.subType" placeholder="请选择" @change="handleQuery">
          <el-option label="轮播图" :value="1"> </el-option>
          <el-option label="广告位" :value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['sys:advertising:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advertisingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告标题" align="center" prop="name" />
      <el-table-column label="广告logo" align="center" prop="logo">
        <template slot-scope="scope">
          <el-image style="width: 60px; height: 60px" :src="baseUrl + scope.row.logo"
            :preview-src-list="[baseUrl + scope.row.logo]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="广告位置" align="center" prop="subType">
        <template slot-scope="scope">
          <div v-if="scope.row.subType == 1">轮播图</div>
          <div v-if="scope.row.subType == 2">广告位</div>
        </template>
      </el-table-column>
      <el-table-column label="跳转路径" align="center" prop="path" />
      <el-table-column label="跳转参数" align="center" prop="params" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">禁用</div>
          <div v-if="scope.row.status == 1">启用</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改广告位配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告位置" prop="subType">
          <el-select v-model="form.subType" placeholder="请选择">
            <el-option label="轮播图" :value="1"> </el-option>
            <el-option label="广告位" :value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入广告位名称" />
        </el-form-item>
        <el-form-item label="上传图片" prop="logo">
          <imageUpload v-model="form.logo" :limit="1" fileWidthHeight="200 * 200 " />
        </el-form-item>
        <el-form-item label="跳转类型" prop="jumpType">
          <el-select v-model="form.jumpType" placeholder="请选择跳转类型">
            <el-option :key="1" label="小程序" :value="1"></el-option>
            <el-option :key="2" label="H5" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入跳转路径" />
        </el-form-item>
        <el-form-item label="跳转参数" prop="params">
          <el-input v-model="form.params" placeholder="请输入跳转参数" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageAdvertising,
  listAdvertising,
  getAdvertising,
  addAdvertising,
  updateAdvertising,
  delAdvertising,
} from "@/api/sys/advertising";

export default {
  name: "Advertising",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告位配置表格数据
      advertisingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: null,
        logo: null,
        type: null,
        subType: null,
        path: null,
        params: null,
        sort: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createUser: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        updateUser: [
          { required: true, message: "更新人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "广告位名称不能为空", trigger: "blur" },
        ],
        logo: [
          { required: true, message: "广告位logo不能为空", trigger: "blur" },
        ],
        jumpType: [
          {
            required: true,
            message: "跳转类型不能为空",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "广告位类型不能为空",
            trigger: "change",
          },
        ],
        subType: [
          {
            required: true,
            message: "广告位位置不能为空",
            trigger: "change",
          },
        ],
        path: [
          { required: true, message: "跳转路径不能为空", trigger: "blur" },
        ],
        params: [
          { required: true, message: "跳转参数不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "状态为空",
            trigger: "blur",
          },
        ],
        deleted: [
          { required: true, message: "删除状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询广告位配置列表 */
    getList() {
      this.loading = true;
      pageAdvertising(this.addDateRange(this.queryParams, this.dateRange)).then(
        (res) => {
          this.advertisingList = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null,
        name: null,
        logo: null,
        type: null,
        subType: null,
        path: null,
        params: null,
        sort: null,
        status: 1,
        deleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告位配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAdvertising(id).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改广告位配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAdvertising(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdvertising(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除广告位配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAdvertising(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/sys/advertising/export",
        {
          ...this.queryParams,
        },
        `advertising_${new Date().getTime()}.xlsx`
      );
    },
    logoChange(list) {
      console.log("list=", list);
      if (list.length > 0) {
        this.form.logo = list[0].url;
      } else {
        this.form.logo = ""
      }
    },
  },
};
</script>
